package com.yt.al.db;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

public class HuaFeiDao extends BaseDao{
	/**
	 * 返回格式为 phone,pwd,phoneid
	 * 
	 * @return
	 */
	public String phoneAndPwd() {
		String phoneAndPwd = "";
		Connection conn = getConnect();
		ResultSet rs;
		try {
			rs = conn.createStatement().executeQuery(
					"select top 1 * from TB_HuaFei where querystate=-1 or (querystate=0 and datediff(mi, usedtime, getdate())>5) order by newid() " );
			if (rs.next()) {
				String phone = rs.getString("phone");
				String pwd = rs.getString("pwd");
				phoneAndPwd = phone + "," + pwd;
				
				updateQueryState(phone, 0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return phoneAndPwd;
	}

	/**
	 * 
	 * @param phone
	 * @param state -1 初始化， 0 开始查询 1 成功 2 失败
	 * @return
	 */
	public boolean updateQueryState(String phone, int state) {
		boolean suc = false;
		Connection conn = getConnect();
		String sql = "";
		if(state==0)
		{
			sql = "update TB_Huafei set querystate="+state+", usedtime=getdate() where phone='"+ phone+"'";
		}
		else
		{
			sql = "update TB_Huafei set querystate="+state+" where phone='"+ phone+"'";
		}
		
		
		try {
			suc = conn.createStatement().execute(sql);
		} catch (Exception ex) {

		} finally {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return suc;
	}
	
	
	public boolean updateHuaFeiSuc(String phone, String huafei) {
		boolean suc = false;
		Connection conn = getConnect();
		String sql = "";
		sql = "update TB_huafei set huafei='"+huafei+"',querytime=getdate(), querystate=1 where phone='"
				+ phone +"'";
		try {
			suc = conn.createStatement().execute(sql);
		} catch (Exception ex) {

		} finally {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return suc;
	}
	
	
	public boolean updateHuaFeiFail(String phone) {
		boolean suc = false;
		Connection conn = getConnect();
		String sql = "";
		sql = "update TB_huafei set querytime=getdate(), querystate=2 where phone='"
				+ phone +"'";
		try {
			suc = conn.createStatement().execute(sql);
		} catch (Exception ex) {

		} finally {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return suc;
	}
}
